$().ready(function () {
    validateRule();
    load();

    var html = "";
    $.ajax({
        url: '/common/dict/type',
        success: function (data) {
            //加载数据
            for (var i = 0; i < data.length; i++) {
                html += '<option value="' + data[i].type + '">' + data[i].description + '</option>'
            }
            $(".chosen-select").append(html);
            $(".chosen-select").chosen({
                maxHeight: 200
            });
        }
    });

});


$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function load() {
    $('#bootstrap-table')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: "/common/genTable/column/list", // 服务器数据的加载地址
                //	showRefresh : true,
                //	showToggle : true,
                //	showColumns : true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParams: function (params) {
                    return {
                        //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset,
                        tableId: $('#tableId').val(),

                        // name:$('#searchName').val(),
                        // username:$('#searchName').val()
                    };
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [{
                    title: "序号",
                    width: "5%",
                    field: 'columnId',
                    formatter: function (value, row, index) {
                        var html = "<input class='form-control' type='text' name='columns[" + index + "].columnId' value='" + value + "' readonly>";
                        return html;
                    }

                },
                    {
                        field: 'columnName',
                        title: '字段列名',
                        width: "10%",
                        class: "nodrag",
                        cellStyle: function (value, row, index) {
                            return {css: {"cursor": "default"}};
                        }
                    },
                    {
                        field: 'columnComment',
                        title: '字段描述',
                        width: "10%",
                        formatter: function (value, row, index) {
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].columnComment' value='" + value + "'>";
                            return html;
                        }
                    },
                    {
                        field: 'columnType',
                        title: '物理类型',
                        width: "10%",
                        class: "nodrag",
                        cellStyle: function (value, row, index) {
                            return {css: {"cursor": "default"}};
                        }
                    },
                    {
                        field: 'javaType',
                        title: 'Java类型',
                        width: "10%",
                        formatter: function (value, row, index) {
                            console.log(value)
                            var data = [{index: index, javaType: value}];
                            var html = "<select class='form-control' name='columns[" + index + "].javaType'>\n";
                            if (value === "Long") {
                                html += " <option value=\"Long\" selected>Long</option>\n";
                            } else if (value != "Long") {
                                html += " <option value=\"Long\" >Long</option>\n";
                            }

                            if (value === "Integer") {
                                html += " <option value=\"Integer\" selected>Integer</option>\n";
                            } else if (value != "Integer") {
                                html += " <option value=\"Integer\" >Integer</option>\n";
                            }

                            if (value === "Double") {
                                html += " <option value=\"Double\" selected>Double</option>\n";
                            } else if (value != "Double") {
                                html += " <option value=\"Double\" >Double</option>\n";
                            }

                            if (value === "BigDecimal") {
                                html += " <option value=\"BigDecimal\" selected>BigDecimal</option>\n";
                            } else if (value != "BigDecimal") {
                                html += " <option value=\"BigDecimal\" >BigDecimal</option>\n";
                            }

                            if (value === "Date") {
                                html += " <option value=\"Date\" selected>Date</option>\n";
                            } else if (value != "Date") {
                                html += " <option value=\"Date\" >Date</option>\n";
                            }

                            if (value === "String") {
                                html += " <option value=\"String\" selected>String</option>\n";
                            } else if (value != "String") {
                                html += " <option value=\"String\" >String</option>\n";
                            }
                            return html;
                        }
                    },
                    {
                        field: 'javaField',
                        title: 'Java属性',
                        width: "10%",
                        formatter: function (value, row, index) {
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].javaField' value='" + value + "'>";
                            return html;
                        }
                    },
                    {
                        field: 'isInsert',
                        title: '插入',
                        width: "5%",
                        formatter: function (value, row, index) {
                            // var isCheck = value == 1 ? 'checked' : '';
                            // var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isInsert' value='1' " + isCheck + "></label>";
                            // return html;


                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isInsert' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isInsert' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;
                        }
                    },
                    {
                        field: 'isEdit',
                        title: '编辑',
                        width: "5%",
                        formatter: function (value, row, index) {
                            // var isCheck = value == 1 ? 'checked' : '';
                            // var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isEdit' value='1' " + isCheck + "></label>";
                            // return html;

                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isEdit' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isEdit' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;
                        }
                    },
                    {
                        field: 'isList',
                        title: '列表',
                        width: "5%",
                        formatter: function (value, row, index) {
                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isList' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isList' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;

                            // var html = "<select class='form-control' name='columns[" + index + "].javaType'>\n";
                            // if (value === "1") {
                            //     html += " <option value=\"1\" selected>是</option>\n";
                            //     html += " <option value=\"0\" >否</option>\n";
                            // } else {
                            //     html += " <option value=\"0\" selected>否</option>\n";
                            //     html += " <option value=\"1\">是</option>\n";
                            // }
                            // html += "</select>";
                            //
                            // return html;
                        }
                    },
                    {
                        field: 'isQuery',
                        title: '查询',
                        width: "5%",
                        formatter: function (value, row, index) {
                            // var isCheck = value == 1 ? 'checked' : '';
                            // var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isQuery' value='1' " + isCheck + "></label>";
                            // return html;

                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isQuery' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isQuery' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;
                        }
                    },   {
                        field: 'isApply',
                        title: '审批',
                        width: "5%",
                        formatter: function (value, row, index) {
                            // var isCheck = value == 1 ? 'checked' : '';
                            // var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isQuery' value='1' " + isCheck + "></label>";
                            // return html;

                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isApply' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isApply' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;
                        }
                    },   {
                        field: 'isReview',
                        title: '查看',
                        width: "5%",
                        formatter: function (value, row, index) {
                            // var isCheck = value == 1 ? 'checked' : '';
                            // var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isQuery' value='1' " + isCheck + "></label>";
                            // return html;

                            var isCheck = value == 1 ? 'checked' : '';
                            var isCheck1 = value == 0 || value == null ? 'checked' : '';
                            var html = "<label class='check-box'>";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isReview' value='1' " + isCheck + ">";
                            html += "<input type='radio' id='columns[" + index + "].isList' name='columns[" + index + "].isReview' value='0' " + isCheck1 + ">";
                            html += "</label>";
                            return html;
                        }
                    },
                    // 在columns数组的最后添加操作列
                    {
                        title: '操作',
                        field: 'operate',
                        width: "20%",  // 增加宽度以容纳三个按钮
                        formatter: function(value, row, index) {
                            // 创建三个按钮
                            var html = '<div class="btn-group">';
                            html += '<button type="button" class="btn btn-danger btn-sm"' +
                                ' onclick="disableAllOptions(' + index + ')">全部禁用</button>';
                            html += '<button type="button" class="btn btn-warning btn-sm"' +
                                ' onclick="disableQueryOnly(' + index + ')">不要查询</button>';
                            html += '<button type="button" class="btn btn-info btn-sm"' +
                                ' onclick="enableListOnly(' + index + ')">仅列表</button>';
                            html += '</div>';
                            return html;
                        }
                    },
                    {
                        field: 'queryType',
                        title: '查询方式',
                        width: "10%",
                        // formatter: function (value, row, index) {
                        //     var html = "<input class='form-control' type='text' name='columns[" + index + "].columnComment' value='" + value + "'>";
                        //     return html;
                        // }
                        // formatter: function (value, row, index) {
                        //     var data = [{index: index, queryType: value}];
                        //     return $("#queryTypeTpl").tmpl(data).html();
                        // }
                    },
                    {
                        field: 'isRequired',
                        title: '必填',
                        width: "5%",
                        formatter: function (value, row, index) {
                            var isCheck = value == 1 ? 'checked' : '';
                            var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].isRequired' value='1' " + isCheck + "></label>";
                            return html;
                        }
                    },
                    {
                        field: 'htmlType',
                        title: '显示类型',
                        width: "12%",
                        formatter: function (value, row, index) {
                            console.log(value)
                            var data = [{index: index, javaType: value}];
                            var html = "<select class='form-control' name='columns[" + index + "].htmlType'>\n";
                            if (value === "input") {
                                html += " <option value=\"input\" selected>input</option>\n";
                            } else if (value != "input") {
                                html += " <option value=\"input\" >input</option>\n";
                            }

                            if (value === "textarea") {
                                html += " <option value=\"textarea\" selected>textarea</option>\n";
                            } else if (value != "textarea") {
                                html += " <option value=\"textarea\" >textarea</option>\n";
                            }

                            if (value === "select") {
                                html += " <option value=\"select\" selected>select</option>\n";
                            } else if (value != "select") {
                                html += " <option value=\"select\" >select</option>\n";
                            }

                            if (value === "selectObject") {
                                html += " <option value=\"selectObject\" selected>selectObject</option>\n";
                            } else if (value != "selectObject") {
                                html += " <option value=\"selectObject\" >selectObject</option>\n";
                            }

                            if (value === "radio") {
                                html += " <option value=\"radio\" selected>radio</option>\n";
                            } else if (value != "radio") {
                                html += " <option value=\"radio\" >radio</option>\n";
                            }

                            if (value === "checkbox") {
                                html += " <option value=\"checkbox\" selected>checkbox</option>\n";
                            } else if (value != "checkbox") {
                                html += " <option value=\"checkbox\" >checkbox</option>\n";
                            }

                            if (value === "datetime") {
                                html += " <option value=\"datetime\" selected>datetime</option>\n";
                            } else if (value != "datetime") {
                                html += " <option value=\"Date\" >datetime</option>\n";
                            }
                            return html;
                        }
                    },
                    {
                        field: 'dictType',
                        title: '字典类型',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].dictType' value='" + value + "'>";
                            return html;
                        }
                    },    {
                        field: 'dictTypePk',
                        title: '字典类型对象主键',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].dictTypePk' value='" + value + "'>";
                            return html;
                        }
                    },    {
                        field: 'dictTypeAttr',
                        title: '字典类型属性回显',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].dictTypeAttr' value='" + value + "'>";
                            return html;
                        }
                    },    {
                        field: 'dateRange',
                        title: '日期范围',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].dateRange' value='" + value + "'>";
                            return html;
                        }
                    },    {
                        field: 'readonly',
                        title: '不可读',
                        width: "5%",
                        formatter: function (value, row, index) {
                            var isCheck = value == 1 ? 'checked' : '';
                            var html = "<label class='check-box'><input type='checkbox' name='columns[" + index + "].readonly' value='1' " + isCheck + "></label>";
                            return html;
                        }
                    },  {
                        field: 'contentApply',
                        title: '对应审批流程哪个流程',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].contentApply' value='" + value + "'>";
                            return html;
                        }
                    }, {
                    field: 'applyTypeShow',
                        title: '审批字段某阶段需编辑',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].applyTypeShow' value='" + value + "'>";
                            return html;
                        }
                    },  {
                        field: 'calStr',
                        title: '数字计算方式',
                        width: "50px",
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = "";
                            }
                            var html = "<input class='form-control' type='text' name='columns[" + index + "].calStr' value='" + value + "'>";
                            return html;
                        }
                    }
                ]
            });
}

// 全部禁用函数（设置插入、编辑、列表、查询都为0）
function disableAllOptions(index) {
    $('input[name="columns[' + index + '].isInsert"][value="0"]').prop('checked', true);
    $('input[name="columns[' + index + '].isEdit"][value="0"]').prop('checked', true);
    $('input[name="columns[' + index + '].isList"][value="0"]').prop('checked', true);
    $('input[name="columns[' + index + '].isQuery"][value="0"]').prop('checked', true);
}

// 仅禁用查询函数（只设置查询为0，保留其他状态）
function disableQueryOnly(index) {
    $('input[name="columns[' + index + '].isQuery"][value="0"]').prop('checked', true);
    // 其他字段保持原状
}

// 仅列表函数（列表设为1，其他都设为0）
function enableListOnly(index) {
    $('input[name="columns[' + index + '].isInsert"][value="0"]').prop('checked', true);
    $('input[name="columns[' + index + '].isEdit"][value="0"]').prop('checked', true);
    $('input[name="columns[' + index + '].isList"][value="1"]').prop('checked', true);
    $('input[name="columns[' + index + '].isQuery"][value="0"]').prop('checked', true);
}function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/common/genTable/update",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入名字"
            }
        }
    })
}